﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using AnimalCare.Model;
using AnimalCare.View;
using AnimalCare.Controller;
using AnimalCare.Service;

namespace AnimalCare.View
{
    public partial class FormClassification : DevExpress.XtraEditors.XtraForm
    {
        public FormClassification()
        {
            InitializeComponent();
            refreshGrid();
            InitGrid();
        }

        void InitGrid()
        {
        }
        protected void refreshGrid()
        {
            AnimalCareDBEntities entities;
            entities = new AnimalCareDBEntities();
            gridClassification.DataSource = entities.Classifications;
            gridClassification.Refresh();
        }
        private void BtnInsertClassification_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(TextClassification.Text))
            {
                ClassificationService.Insert(TextClassification.Text);
                refreshGrid();
            }
        }

        private void deleteMenu_Click(object sender, EventArgs e)
        {
            int[] selectedRowsIndex = gridViewClass.GetSelectedRows();
            if (selectedRowsIndex.Length > 0)
            {
                int indexRow = selectedRowsIndex[0];
                Classification cl = (Classification)gridViewClass.GetRow(indexRow);
                ClassificationService.Delete(cl.classification_id);
                refreshGrid();
            }
        }

        private void updateMenu_Click(object sender, EventArgs e)
        {
            int[] selectedRowsIndex = gridViewClass.GetSelectedRows();
            if (selectedRowsIndex.Length > 0)
            {
                int indexRow = selectedRowsIndex[0];
                Classification cl = (Classification)gridViewClass.GetRow(indexRow);
                ClassificationService.Update(cl);
                refreshGrid();
            }
            else
            {
                ValidateService.validateEmpty(TextClassification, errorProvider1, "Chưa nhập phân loại");
            }
        }

        private void FormClassification_Load(object sender, EventArgs e)
        {
        }

        private void TextClassification_TextChanged(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(TextClassification.Text))
            {
            }
            else
            {
            }
        }
    }
}
